package com.web.servlet;


import com.entity.Clerk;
import com.result.QueryResult;
import com.result.Result;
import com.result.ResultUtils;
import com.service.ClerkService;
import com.service.impl.ClerkServiceImpl;
import com.util.JsonUtils;
import com.util.ResponseUtils;
import com.util.WebUtils;
import com.vo.QueryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author lenovo
 */
@WebServlet(name = "ClerkServlet", urlPatterns = "/clerk/*")
public class ClerkServlet extends HttpServlet {

    private ClerkService clerkService=new ClerkServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String uri = request.getRequestURI();
            String methodName = uri.substring(uri.lastIndexOf("/") + 1);
            if (methodName.equals("list")) {
                list(request, response);
            }
            if (methodName.equals("add")) {
                add(request, response);
            }
            if (methodName.equals("update")) {
                update(request, response);
            }
            if (methodName.equals("delete")) {
                delete(request, response);
            }
            if(methodName.equals("search")){
                search(request,response);
            }
        }catch (Exception e){
            e.printStackTrace();
            Result result = ResultUtils.buildFail(50000,"对不起，系统繁忙，待会再来!");
            ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),response);
        }
    }

    private void search(HttpServletRequest request, HttpServletResponse response) {
        //1.接收条件
        QueryVo queryVo = WebUtils.requestToBean(request, QueryVo.class);
        //2.执行查询 开始序号 （当前页数-1）*分页大小
        String searchName = request.getParameter("searchName");
        QueryResult queryResult =clerkService.searchLikeName(searchName,queryVo.getStartIndex(), queryVo.getLimit());
        //3.构建返回结果
        Result result = ResultUtils.buildSuccess(queryResult.getList(),queryResult.getCount());
        //转换成json，发送给前端
        //发送json给客户端
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),response);


    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        //1.接收客户端的参数
        String id = request.getParameter("id");
        //2.调用service进行处理
        clerkService.delete(Integer.valueOf(id));
        //3.返回删除成功的结果
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        //1.接收客户端的参数
        Clerk clerk = WebUtils.requestToBean(request, Clerk.class);
        //2.提交添加请求
        clerkService.update(clerk);
        //3.返回添加修改的信息
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess(null,1L)),response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        // 1.接收客户端的参数
        Clerk clerk=WebUtils.requestToBean(request,Clerk.class);

        //2.提交添加请求
        clerkService.add(clerk);

        //3.返回添加成功的信息
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess(null,1L)),response);

    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
        //1.接收条件
        QueryVo queryVo = WebUtils.requestToBean(request, QueryVo.class);
        //2.执行查询 开始序号 （当前页数-1）*分页大小
        QueryResult queryResult =clerkService.selectByPage(queryVo.getStartIndex(), queryVo.getLimit());
        //3.构建返回结果
        Result result = ResultUtils.buildSuccess(queryResult.getList(),queryResult.getCount());
        //转换成json，发送给前端
        //发送json给客户端
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),response);

    }

}
